package cn.itcast.manage;

import cn.itcast.bean.User;
import cn.itcast.dao.AddServlet;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/addUser")
public class AddUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String age = req.getParameter("age");
        int i = Integer.parseInt(age);
        User user = new User();
        user.setName(username);
        user.setAge(i);
        AddServlet addServlet = new AddServlet();
        addServlet.insert(user);
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write("保存成功");
        resp.setHeader("refresh","1;URL=/index.jsp");


    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
